﻿using CommunityToolkit.Maui.Alerts;
using Google.Protobuf.WellKnownTypes;
using System.Diagnostics;

namespace Elio.Sessions;

public class ActivitySession : BaseSession
{
    protected override string SessionName => base.SessionName = "Activity";

    protected override async Task OnCreate(long sid = 0, long fid = 0, params Any[] argv) =>
       await base.OnCreate(sid, fid, argv);

    protected override async Task OnReceive(string name, long sessionId, long futureId, params Any[] argv)
    {
        switch (name)
        {
            case "getScreenSize":
                await GetScreenSize(sessionId, futureId);
                break;

            case "getTitle":
                await GetTitle(sessionId, futureId);
                break;

            case "setTitle":
                SetTitle(argv);
                break;

            case "accessibilityNotice":
                await AccessibilityNotice(argv);
                break;

            case "setTouchExploration":
                SetTouchExploration();
                break;

            case "getClipboardText":
                await GetClipboardText(sessionId, futureId);
                break;

            case "setClipboardText":
                await SetClipboardText(argv);
                break;

            case "isScrollable":
                IsScrollable();
                break;

            case "setScrollable":
                SetScrollable();
                break;

            case "vibrate":
                Vibrate(argv);
                break;

            default:
                await base.OnReceive(name, sessionId, futureId, argv);
                break;
        }
    }

    private async Task GetScreenSize(long sid, long fid)
    {
        Size size = ElegantPage!.PageSize!.Value;
        (int width, int height) = ((int)size.Width, (int)size.Height);
        await SendAction("onGetScreenSize", sid, fid, width.ToAny(), height.ToAny());
    }

    private async Task GetTitle(long sid, long fid) =>
       await SendAction("onGetTitle", sid, fid, ElegantPage!.Title.ToAny());
    private void SetTitle(params Any[] argv) =>
       ElegantPage!.Title = argv[0].P2String();

    private static async Task AccessibilityNotice(params Any[] argv) =>
       await Toast.Make(argv[0].P2String()).Show();

    private static void SetTouchExploration() { }

    private async Task GetClipboardText(long sid, long fid)
    {
        string text = await Clipboard.Default.GetTextAsync() ?? string.Empty;
        await SendAction("onGetClipboardText", sid, fid, text.ToAny());
    }

    private static async Task SetClipboardText(params Any[] argv) =>
       await Clipboard.Default.SetTextAsync(argv[0].P2String());

    private static void IsScrollable() { }

    private static void SetScrollable() { }

    private static void Vibrate(params Any[] argv) =>
       //int _ = (int)argv[1]; // amplitude 在Maui中不能使用
       Vibration.Default.Vibrate(TimeSpan.FromMilliseconds(argv[0].P2Long()));

    public async Task Connect()
    {
        DestroyEvent -= OnConnect;
        DestroyEvent += OnConnect;
        await OnCreate();
    }
    private async void OnConnect(object? s, EventArgs e)
    {
        await Task.Delay(5000);
        await Connect();
    }

    public async Task GoBack()
    {
        TaskCompletionSource<Any> tcs = new();
        await SendActionUntilCallbackResult(tcs, "onBackPressed");
        bool res = (await tcs.Task).P2Bool();

        Debug.WriteLine($"### BackPressed Result: {res}");
    }
}